package com.lb.jwitter.service;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.*;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;



@Service("tbl_ThongTin_NhomLop_MonHocService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_ThongTin_NhomLop_MonHocService")
public class Tbl_ThongTin_NhomLop_MonHocServiceImpl implements Tbl_ThongTin_NhomLop_MonHocService,Serializable {

   private Log log = LogFactory.getLog(Tbl_ThongTin_NhomLop_MonHocServiceImpl.class);
   private static final long serialVersionUID = -8740095022264495229L;

   @Autowired
   @Qualifier("tbl_ThongTin_NhomLop_MonHocDAO")
   private Tbl_ThongTin_NhomLop_MonHocDAO _tbl_ThongTin_NhomLop_MonHocDao;

   @Autowired
   @Qualifier("tbl_DKMH_DotDKDAO")
   private Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao;

   @Autowired
   @Qualifier("tbl_CTDT_MonHocDAO")
   private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

   @Autowired
   @Qualifier("tbl_ThongTin_TKBDAO")
   private Tbl_ThongTin_TKBDAO _tbl_ThongTin_TKBDao;

   @Autowired
   @Qualifier("tbl_ThongTin_TKB_ChiTietDAO")
   private Tbl_ThongTin_TKB_ChiTietDAO _tbl_ThongTin_TKB_ChiTietDAO;

   @Autowired
   @Qualifier("tbl_CTDT_DM_PhongDAO")
   private Tbl_CTDT_DM_PhongDAO _tbl_CTDT_DM_PhongDao;

   @Autowired
   @Qualifier("tbl_CTDT_DM_NgayTrongTuanDAO")
   private Tbl_CTDT_DM_NgayTrongTuanDAO _tbl_CTDT_DM_NgayTrongTuanDao;

   @Autowired
   @Qualifier("tbl_CTDT_DM_TietHocDAO")
   private Tbl_CTDT_DM_TietHocDAO _tbl_CTDT_DM_TietHocDao;

   @Autowired
   @Qualifier("tbl_CanBoDAO")
   private Tbl_CanBoDAO _tbl_CanBoDao;

   public final List<Tbl_ThongTin_NhomLop_MonHoc> findAlltbl_ThongTin_NhomLop_MonHoc(int pageSize, int page){
   List<Tbl_ThongTin_NhomLop_MonHoc> result = get_tbl_ThongTin_NhomLop_MonHocDao().findAll(pageSize, page);
       log.info("Number of user in DB:: "+result.size());
       return result;
   }

   @Transactional(noRollbackFor=IllegalArgumentException.class)
   public final Long createtbl_ThongTin_NhomLop_MonHoc(Tbl_ThongTin_NhomLop_MonHoc obj){
       return _tbl_ThongTin_NhomLop_MonHocDao.save(obj);
   }
   
   /*
    * HAM TU DONG TAO THOI KHOA BIEU TU CHUOI JSON
    * @dotDKId : ma dot Dang ky
    * @data: chuoi string theo format JSON
    * @taiKhoan : thong tin tai khoan nguoi tao TKB
    */
	public void createListtbl_ThongTin_NhomLop_MonHocByJSON(int dotDKId, String data, Tbl_TaiKhoan taiKhoan){
		try{
		ChumanoLogger.init(Tbl_ThongTin_NhomLop_MonHocServiceImpl.class.getName());
		ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
		
		List<Tbl_CTDT_DM_NgayTrongTuan> ngays = _tbl_CTDT_DM_NgayTrongTuanDao.findAll(0, 0);
		List<Tbl_CTDT_DM_TietHoc> tietHocs = _tbl_CTDT_DM_TietHocDao.findAll(0, 0);
		
		//tao danh sach chi tiet insert
		List<Tbl_ThongTin_TKB_ChiTiet> tbl_ThongTin_TKB_ChiTiets = new ArrayList<Tbl_ThongTin_TKB_ChiTiet>();
		//danh sach nhom lop mon hoc 
		List<Tbl_ThongTin_NhomLop_MonHoc> tbl_ThongTin_NhomLop_MonHocs = new ArrayList<Tbl_ThongTin_NhomLop_MonHoc>();
		
		//tao doi tuong dotDK
		Tbl_DKMH_DotDK tbl_DKMH_DotDK = new Tbl_DKMH_DotDK();
		tbl_DKMH_DotDK.setId(new Long(dotDKId));
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
        List<String> columns = new ArrayList<String>();
        
		JSONArray jsons = (JSONArray) JSONSerializer.toJSON( data );
		
		for(int i = 0; i < jsons.size() ; i++){
			JSONObject obj = jsons.getJSONObject(i);
			String logStr="";
			//----------------------------------------------
			//Lay du lieu
			String maNhom = obj.getString("MANHOM");
			String maMonHoc = obj.getString("MAMONHOC");
			String maNgayHoc = obj.getString("MANGAYHOC");
			String maPhongHoc = obj.getString("MAPHONGHOC");
			String maTietBatDau = obj.getString("TIETBATDAU");
			
			//Tuan hoc
			int sotuanhoc = 26;
			int tuanhoc = 0;
			for (int j = 1; j <= sotuanhoc; j++) {
				String hoc = obj.getString("T" + j);
				if (hoc.equals("x")) {
					tuanhoc += (int) Math.pow(2, sotuanhoc - j);
				}
			}
			
			logStr ="[ " + (i+1)+ " - " 
				+ "NHOM: " + maNhom + " - " 
				+ "MONHOC: " + maMonHoc + " - " 
				+ "NGAY: " + maNgayHoc + " - " 
				+ "PHONG: " + maPhongHoc + " - " 
				+ "TIETBD: " + maTietBatDau + "]"
				+ "TUANHOC: " + Integer.toBinaryString(tuanhoc);
			//----------------------------------------------
			//==============================================
			if(!maNhom.equals("") && !maMonHoc.equals("") && !maNgayHoc.equals("") && !maPhongHoc.equals("") && !maTietBatDau.equals("")){
				//kiem tra nhom lop mon hoc da co chua
				boolean nhomLopExistYN = false;
				for(Tbl_ThongTin_NhomLop_MonHoc nhomLop : tbl_ThongTin_NhomLop_MonHocs){
					
					//nhom lop mon hoc da ton tai
					if(nhomLop.getMa().equals(maNhom) 
							&& nhomLop.getMonHocId().getMa().equals(maMonHoc)
					){
							nhomLopExistYN = true;	
							break;
					}//endif
				
				}//endfor kiem tra nhom lop da ton tai chua
				
				
				/*NEU NHOM LOP CHUA TON TAI
				 * Neu nhom lop cung thoi khoa bieu voi nhom khac
				 * thi them moi nhom lop va tro den TKB da co
				 * 
				 * Neu chua co thoi khoa bieu thi lay tat ca cac chi tiet TKB cua nhom lop
				 * sau do tao thoi khoa bieu moi voi cac chi tiet la chi tiet TKB cua nhom lop hien tai
				 * tao nhom lop moi tro den TKB vua tao
				 */
				if(!nhomLopExistYN){
					//lay ra thoi khoa bieu cua nhom lop
					List<JSONObject> tkbChiTiets = new ArrayList<JSONObject>();
					//tkbChiTiets.add(obj);
					for(int j = i; j < jsons.size() ; j++){
						JSONObject obj2 = jsons.getJSONObject(j);
						if(maNhom.equals(obj2.getString("MANHOM"))&& maMonHoc.equals(obj2.getString("MAMONHOC"))){
							tkbChiTiets.add(obj2);
						}
					}
					
					/*
					 * kiem tra thoi khoa bieu da co chua
					 * Neu co thi chi can cap nhat nhom lop vao TKB da ton tai trc do
					 * Neu chua co thi tao moi TKB voi cac chi tiet cua nhom lop hien tai
					 */
					boolean tkbChiTietExistYN = false;
					for(Tbl_ThongTin_TKB_ChiTiet chiTiet : tbl_ThongTin_TKB_ChiTiets){
						for(JSONObject tkbChiTiet : tkbChiTiets){
							if(chiTiet.getNgayHocId()!= null 
									&& chiTiet.getNgayHocId().getMa().equals(tkbChiTiet.getString("MANGAYHOC"))
								 && chiTiet.getPhongHocId() !=null 
								 && chiTiet.getPhongHocId().getMa().equals(tkbChiTiet.getString("MAPHONGHOC"))
								&& chiTiet.getTietBatDauId() != null 
								&& chiTiet.getTietBatDauId().getMa().equals(tkbChiTiet.getString("TIETBATDAU"))
							){
								
								tkbChiTietExistYN = true;
								Tbl_ThongTin_NhomLop_MonHoc tbl_ThongTin_NhomLop_MonHoc = new Tbl_ThongTin_NhomLop_MonHoc();
								tbl_ThongTin_NhomLop_MonHoc.setDotDKId(tbl_DKMH_DotDK);
								tbl_ThongTin_NhomLop_MonHoc.setCanBoId(taiKhoan.getCanBoId());
								tbl_ThongTin_NhomLop_MonHoc.setMa(maNhom);
								
						        
						        tbl_ThongTin_NhomLop_MonHoc.setNgayCapNhat(new Date());
						        tbl_ThongTin_NhomLop_MonHoc.setTKBId(chiTiet.getTKBId());
						        
								
								//set monhoc
								cons.clear();
								{
						        	 ConditionReport con = new ConditionReport(); 
						             con.setCol("ma");
						             con.setValue1(maMonHoc);
						             con.setOperator("eq");
						             cons.add(con);	
						        }
						        columns.clear();
						        columns.add("id");
						        
						        List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class,cons,columns, 1, 1);
						        
						        //set mon hoc
						        if(monHocId != null && monHocId.size() > 0){
						        	Tbl_ThongTin_MonHoc tblThongTinMonHoc = new Tbl_ThongTin_MonHoc();
						        	
						        	Tbl_CTDT_MonHoc monHoc = new Tbl_CTDT_MonHoc();
						        	monHoc.setId(monHocId.get(0));
						        	monHoc.setMa(maMonHoc);
	
						        	tbl_ThongTin_NhomLop_MonHoc.setMonHocId(monHoc);
	
							        tbl_ThongTin_NhomLop_MonHocs.add(tbl_ThongTin_NhomLop_MonHoc);
						        }
								
								break;
							}
						}
						if(tkbChiTietExistYN) break;
					}//ENDFOR kiem tra trong chitietTKB da co hay chua
					
					/*
					 * Neu chua co thoi khoa bieu
					 * Them thoi khoa bieu moi
					 * Them cac chi tiet cho TKB moi tao
					 * Them nhom lop mon hoc thuoc TKB moi tao
					 */
					if(!tkbChiTietExistYN){
						
						//them moi TKB
						Tbl_ThongTin_TKB tbl_ThongTin_TKB = new Tbl_ThongTin_TKB();
						tbl_ThongTin_TKB.setCanBoId(taiKhoan.getCanBoId());
						tbl_ThongTin_TKB.setDotDKId(tbl_DKMH_DotDK);
						tbl_ThongTin_TKB.setNgayCapNhat(new Date());
						try{
						tbl_ThongTin_TKB.setSiSoMax(Integer.parseInt(obj.getString("SISOMAX")));
						}catch(Exception e){
	//						e.printStackTrace();
							tbl_ThongTin_TKB.setSiSoMax(0);
						}
						tbl_ThongTin_TKB.setSiSoMin(0);
						tbl_ThongTin_TKB.setSoHVDangKy(0);
						try{
						tbl_ThongTin_TKB.setTuanBatDau(Integer.parseInt(obj.getString("TUANBATDAU")));
						}catch(Exception e){
	//						e.printStackTrace();
							tbl_ThongTin_TKB.setTuanBatDau(1);
						}
						
						Long tkbId = _tbl_ThongTin_TKBDao.save(tbl_ThongTin_TKB);
						tbl_ThongTin_TKB.setId(tkbId);
						//them nhom lop mon hoc
						Tbl_ThongTin_NhomLop_MonHoc tbl_ThongTin_NhomLop_MonHoc = new Tbl_ThongTin_NhomLop_MonHoc();
						tbl_ThongTin_NhomLop_MonHoc.setDotDKId(tbl_DKMH_DotDK);
						tbl_ThongTin_NhomLop_MonHoc.setCanBoId(taiKhoan.getCanBoId());
						tbl_ThongTin_NhomLop_MonHoc.setMa(maNhom);
				        
				        tbl_ThongTin_NhomLop_MonHoc.setNgayCapNhat(new Date());
				        tbl_ThongTin_NhomLop_MonHoc.setTKBId(tbl_ThongTin_TKB);
				        
						
						//set monhoc
						cons.clear();
						{
				        	 ConditionReport con = new ConditionReport(); 
				             con.setCol("ma");
				             con.setValue1(maMonHoc);
				             con.setOperator("eq");
				             cons.add(con);	
				        }
				        columns.clear();
				        columns.add("id");
				        
				        List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class,cons,columns, 1, 1);
			
				        if(monHocId != null && monHocId.size() > 0){
				        	Tbl_ThongTin_MonHoc tblThongTinMonHoc = new Tbl_ThongTin_MonHoc();
				        	
				        	Tbl_CTDT_MonHoc monHoc = new Tbl_CTDT_MonHoc();
				        	monHoc.setId(monHocId.get(0));
				        	monHoc.setMa(maMonHoc);
	
				        	tbl_ThongTin_NhomLop_MonHoc.setMonHocId(monHoc);				        	
	
					        tbl_ThongTin_NhomLop_MonHocs.add(tbl_ThongTin_NhomLop_MonHoc);
				        }
	
						
						//Them cac chi tiet TKB
						for(JSONObject tkbChiTiet : tkbChiTiets){
							Tbl_ThongTin_TKB_ChiTiet tbl_ThongTin_TKB_ChiTiet = new Tbl_ThongTin_TKB_ChiTiet();
					        //set so tiet
					        try{
					        	tbl_ThongTin_TKB_ChiTiet.setSoTietHoc(Integer.parseInt(tkbChiTiet.getString("SOTIET")));
					        }catch(Exception e){
	
					        	tbl_ThongTin_TKB_ChiTiet.setSoTietHoc(0);
					        
					        }
					        
					        //set thoi khoa bieu
					        tbl_ThongTin_TKB_ChiTiet.setTKBId(tbl_ThongTin_TKB);
					        
							columns.clear();
					        columns.add("id");
					        //get phong hoc
				        	cons.clear();
				        	{
					        	 ConditionReport con = new ConditionReport(); 
					             con.setCol("ma");
					             con.setValue1(tkbChiTiet.getString("MAPHONGHOC"));
					             con.setOperator("eq");
					             cons.add(con);	
					        }
				        	List<Long> phongId = _tbl_CTDT_DM_PhongDao.findColumnByCondition(Tbl_CTDT_DM_Phong.class, cons, columns, 1, 1);
				        	
				        	for(Tbl_CTDT_DM_TietHoc tietHoc : tietHocs){
				        		if(tietHoc.getMa().equals(tkbChiTiet.getString("TIETBATDAU"))){
				        			tbl_ThongTin_TKB_ChiTiet.setTietBatDauId(tietHoc);
				        			break;
				        		}
				        	}
				        	
				        	for(Tbl_CTDT_DM_NgayTrongTuan ngay : ngays){
				        		if(ngay.getMa().equals(tkbChiTiet.getString("MANGAYHOC"))){
				        			tbl_ThongTin_TKB_ChiTiet.setNgayHocId(ngay);
				        			break;
				        		}
				        	}
					        if(phongId != null && phongId.size()>0){
					        	
					        	//set phong
				        		Tbl_CTDT_DM_Phong phong = new Tbl_CTDT_DM_Phong();
				        		phong.setId(phongId.get(0));
				        		phong.setMa(tkbChiTiet.getString("MAPHONGHOC"));
				        		
				        		tbl_ThongTin_TKB_ChiTiet.setPhongHocId(phong);
					            
					        }
					        //them vao list
					        //phong hoc- ngay hoc- tiet bat dau co the Null
					        tbl_ThongTin_TKB_ChiTiets.add(tbl_ThongTin_TKB_ChiTiet);
					    
	
						}//end for chitiettkb
					}//end if chua co TKB
				}//end if nhomlop chua ton tai
			}//end if cac du lieu la null
			
			
			ChumanoLogger.log(logStr);
		}//endfor json
		
		//insert chi tiet TKB
		_tbl_ThongTin_TKB_ChiTietDAO.saveOrUpdateAll(tbl_ThongTin_TKB_ChiTiets);
		//insert nhom lop mon hoc
		_tbl_ThongTin_NhomLop_MonHocDao.saveOrUpdateAll(tbl_ThongTin_NhomLop_MonHocs);
		}
		catch (Exception e) {
			// TODO: handle exception
			   ChumanoLogger.log("ERROR: " +e.getMessage());
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
		}
		   
		ChumanoLogger.stop();

	}


	//CHUMANO
	public void createListtbl_ThongTin_NhomLop_MonHocByJSON2(int dotDKId, String data, Tbl_TaiKhoan taiKhoan){
		try{
		ChumanoLogger.init(Tbl_ThongTin_NhomLop_MonHocServiceImpl.class.getName());
		ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
		
		List<Tbl_CTDT_DM_NgayTrongTuan> ngays = _tbl_CTDT_DM_NgayTrongTuanDao.findAll(0, 0);
		List<Tbl_CTDT_DM_TietHoc> tietHocs = _tbl_CTDT_DM_TietHocDao.findAll(0, 0);
		
		//tao danh sach chi tiet insert
		List<Tbl_ThongTin_TKB_ChiTiet> tbl_ThongTin_TKB_ChiTiets = new ArrayList<Tbl_ThongTin_TKB_ChiTiet>();
		//tao doi tuong dotDK
		Tbl_DKMH_DotDK tbl_DKMH_DotDK = new Tbl_DKMH_DotDK();
		tbl_DKMH_DotDK.setId(new Long(dotDKId));
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
        List<String> columns = new ArrayList<String>();
        
        //T?o MapCollection cho tung hang
		JSONArray jsons = (JSONArray) JSONSerializer.toJSON( data );
		Multimap<String, TT_NhomLop_MonHoc> dics = ArrayListMultimap.create();
		
		ChumanoLogger.log("---LAY VA KIEM TRA DU LIEU---");
		for(int i = 0; i < jsons.size() ; i++){
			JSONObject obj = jsons.getJSONObject(i);
			String logStr="";
			//----------------------------------------------
			//Lay du lieu
			//F_MAMH	F_MANH	F_THU
			//F_TIETBD	F_SOTIET	F_TENPH								
			String maMonHoc = obj.getString("F_MAMH");
			String maNhom = obj.getString("F_MANH");
			
			String maPhongHoc = obj.getString("F_TENPH");
			
			String maNgayHoc = obj.getString("F_THU");
			String maTietBatDau = obj.getString("F_TIETBD");
			String strSoTiet=  obj.getString("F_SOTIET");
			
			int soTiet = Integer.parseInt(strSoTiet.isEmpty()?"0":strSoTiet);
			//Tuan hoc
			int sotuanhoc = 26;
			int tuanhoc = 0;
			for (int j = 1; j <= sotuanhoc; j++) {
				String hoc = obj.getString("T" + j);
				if (hoc.equals("x")) {
					tuanhoc += (int) Math.pow(2, sotuanhoc - j);
				}
			}
			
			logStr ="[ " + (i+1)+ " - " 
				+ "MONHOC: " + maMonHoc + " - " 
				+ "NHOM: " + maNhom + " - " 
				+ "NGAY: " + maNgayHoc + " - " 
				+ "PHONG: " + maPhongHoc + " - " 
				+ "TIETBD: " + maTietBatDau + " - " 
				+ "SOTIET: " + soTiet
				+ "TUANHOC: " + Integer.toBinaryString(tuanhoc)
				+ "]";
			//----------------------------------------------
			//==============================================
			//KIEM TRA DU LIEU
			Tbl_CTDT_MonHoc monhoc= findMonHoc(maMonHoc);
			if(monhoc==null){
				ChumanoLogger.log(logStr+ " <NOT MONHOC>");
				continue;
			}
			
			Tbl_ThongTin_NhomLop_MonHoc tbl_ThongTin_NhomLop_MonHoc = findNhomLopMonHoc(new Long(dotDKId), maMonHoc, maNhom);
			if(tbl_ThongTin_NhomLop_MonHoc==null){
				ChumanoLogger.log(logStr+ " <NOT NHOMLOP-MONHOC>");
				continue;
			}
			
			Tbl_CTDT_DM_NgayTrongTuan ngayhoc = findNgayHoc(maNgayHoc, ngays);
			Tbl_CTDT_DM_Phong phonghoc = findPhongHoc(maPhongHoc);
			Tbl_CTDT_DM_TietHoc tietBD = findTietHoc(maTietBatDau,tietHocs);
			//----------------------------------------------
			//==============================================
			//xoa TKB chitiet cu
			List<Long> chitiets = findTKBChiTiet(tbl_ThongTin_NhomLop_MonHoc.getTKBId().getId());
			for(Long ct : chitiets){
				_tbl_ThongTin_TKB_ChiTietDAO.delete(ct);
			}
			
			//--------tao TKB chitiet---------------
			Tbl_ThongTin_TKB_ChiTiet tbl_ThongTin_TKB_ChiTiet = new Tbl_ThongTin_TKB_ChiTiet();
	        //so tiet
			tbl_ThongTin_TKB_ChiTiet.setSoTietHoc(soTiet);
			
			tbl_ThongTin_TKB_ChiTiet.setNgayHocId(ngayhoc);
	        tbl_ThongTin_TKB_ChiTiet.setPhongHocId(phonghoc);
	        tbl_ThongTin_TKB_ChiTiet.setTietBatDauId(tietBD);
	        tbl_ThongTin_TKB_ChiTiet.setTuanHoc(tuanhoc); //tuan hoc
	        
	        //thoi khoa bieu
	        tbl_ThongTin_TKB_ChiTiet.setTKBId(tbl_ThongTin_NhomLop_MonHoc.getTKBId());
	        tbl_ThongTin_TKB_ChiTiet.setMaCoSo(tbl_ThongTin_NhomLop_MonHoc.getTKBId().getMaCoSo());
			
	        //===them===
	        tbl_ThongTin_TKB_ChiTiets.add(tbl_ThongTin_TKB_ChiTiet);
	        
			ChumanoLogger.log(logStr);
		}//endfor json

		//insert chi tiet TKB
		_tbl_ThongTin_TKB_ChiTietDAO.saveOrUpdateAll(tbl_ThongTin_TKB_ChiTiets);
		
		}
		catch (Exception e) {
			// TODO: handle exception
			   ChumanoLogger.log("--------ERROR: " +e.toString());
			   
			   StringWriter errors = new StringWriter();
			   e.printStackTrace(new PrintWriter(errors));
			   ChumanoLogger.log(errors.toString());
			   ChumanoLogger.log("---------------------------------");
			   
			   //
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
			   
		}
		   
		ChumanoLogger.stop();

	}
	
	//old
	public void createListtbl_ThongTin_NhomLop_MonHocByJSON_old(int dotDKId, String data, Tbl_TaiKhoan taiKhoan){
		try{
		ChumanoLogger.init(Tbl_ThongTin_NhomLop_MonHocServiceImpl.class.getName());
		ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
		
		List<Tbl_CTDT_DM_NgayTrongTuan> ngays = _tbl_CTDT_DM_NgayTrongTuanDao.findAll(0, 0);
		List<Tbl_CTDT_DM_TietHoc> tietHocs = _tbl_CTDT_DM_TietHocDao.findAll(0, 0);
		
		//tao danh sach chi tiet insert
		List<Tbl_ThongTin_TKB_ChiTiet> tbl_ThongTin_TKB_ChiTiets = new ArrayList<Tbl_ThongTin_TKB_ChiTiet>();
		//danh sach nhom lop mon hoc 
		List<Tbl_ThongTin_NhomLop_MonHoc> tbl_ThongTin_NhomLop_MonHocs = new ArrayList<Tbl_ThongTin_NhomLop_MonHoc>();
		
		//tao doi tuong dotDK
		Tbl_DKMH_DotDK tbl_DKMH_DotDK = new Tbl_DKMH_DotDK();
		tbl_DKMH_DotDK.setId(new Long(dotDKId));
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
        List<String> columns = new ArrayList<String>();
        
        //T?o MapCollection cho tung hang
		JSONArray jsons = (JSONArray) JSONSerializer.toJSON( data );
		Multimap<String, TT_NhomLop_MonHoc> dics = ArrayListMultimap.create();
		
		ChumanoLogger.log("---LAY VA KIEM TRA DU LIEU---");
		for(int i = 0; i < jsons.size() ; i++){
			JSONObject obj = jsons.getJSONObject(i);
			String logStr="";
			//----------------------------------------------
			//Lay du lieu
			//MAMONHOC	MANHOM	MAPHONGHOC	MANGAYHOC	
			//SOTIET	TIETBATDAU	SISOMAX	SODK	TUANBATDAU	CUNGTKB								
			String maMonHoc = obj.getString("MAMONHOC");
			String maNhom = obj.getString("MANHOM");
			
			String maNgayHoc = obj.getString("MANGAYHOC");
			String maPhongHoc = obj.getString("MAPHONGHOC");
			String maTietBatDau = obj.getString("TIETBATDAU");
			
			String soTiet = obj.getString("SOTIET");
			String siso = obj.getString("SISOMAX");
			String soDK = obj.getString("SODK");
			String tuanBD = obj.getString("TUANBATDAU");
			
			String cungTKB = obj.getString("CUNGTKB");
			
			logStr ="[ " + (i+1)+ " - " 
				+ "MONHOC: " + maMonHoc + " - " 
				+ "NHOM: " + maNhom + " - " 
				+ "NGAY: " + maNgayHoc + " - " 
				+ "PHONG: " + maPhongHoc + " - " 
				+ "TIETBD: " + maTietBatDau + " - " 
				+ "SOTIET: " + soTiet + " - " 
				+ "SISO: " + siso + " - " 
				+ "SODK: " + soDK + " - " 
				+ "TUANBD: " + tuanBD 
				+ "]";
			//----------------------------------------------
			//==============================================
			//KIEM TRA DU LIEU
			Tbl_CTDT_DM_NgayTrongTuan ngayhoc = findNgayHoc(maNgayHoc, ngays);
			Tbl_CTDT_DM_Phong phonghoc = findPhongHoc(maPhongHoc);
			Tbl_CTDT_DM_TietHoc tietBD = findTietHoc(maTietBatDau,tietHocs);
			
			Tbl_CTDT_MonHoc monhoc= findMonHoc(maMonHoc);
			if(monhoc==null){
				ChumanoLogger.log(logStr+ " <NOT MONHOC>");
				continue;
			}
			//----------------------------------------------
			//luu cac doi tuong nay l?i
			TT_NhomLop_MonHoc tt = new TT_NhomLop_MonHoc();
			tt.setMonHoc(monhoc);
			tt.setMaNhom(maNhom);
			
			tt.setNgayHoc(ngayhoc);
			tt.setPhongHoc(phonghoc);
			tt.setTietBatDau(tietBD);
			
			tt.setSoTiet(soTiet);
			tt.setSiso(siso);
			tt.setSoDK(soDK);
			tt.setTuanBD(tuanBD);
			
			tt.setCungTKB(cungTKB);
			
			dics.put(maMonHoc+"_"+ maNhom, tt);
			
			ChumanoLogger.log(logStr);
		}//endfor json
		ChumanoLogger.log("===[KIEM TRA DU LIEU] FINISHED===");
		/*
		Multiset<String> keys = dics.keys();
		for(String key: keys){
			ChumanoLogger.log("[ " +key +" ]");
			Collection<TT_NhomLop_MonHoc> tts = dics.get(key);
			for(TT_NhomLop_MonHoc tt : tts)
				ChumanoLogger.log("< " +tt.getSiso() +" >");
		}
		*/
		
		//===================================================================
		//-------------------------------------------------------------------
		int count = 0;
		ChumanoLogger.log("---THEM DU LIEU---");
		while(dics!=null && !dics.isEmpty()){
			String logStr ="";
			//----------Lay 1 doi tuong bat ky------------
			Multiset<String> keys = dics.keys();
			if(keys==null){
				ChumanoLogger.log("ALL KEYS : NULL");		
			}
			
			String key = null;
			ChumanoLogger.log("SIZE: " +keys.size());
			for(String k: keys){
				key = k;
				break;
			}
			//--------------------------------------------
			if(key==null){
				ChumanoLogger.log("GET FIRST : NULL");
				break;
			}
			logStr ="[COUNT: " + (count+1) +"]: " +  key;
			//--------------------------------
			Collection<TT_NhomLop_MonHoc> nlmhs= dics.get(key);
			
			if(nlmhs==null||nlmhs.size()==0){
				dics.removeAll(key);
				ChumanoLogger.log(logStr);
				continue;
			}
			//-----Tao TKB------
			boolean isfirst = true;
			Long tkbid;
			Tbl_ThongTin_TKB tbl_ThongTin_TKB = null;
			TT_NhomLop_MonHoc firstTT =null;
			for(TT_NhomLop_MonHoc tt:nlmhs){
				if(isfirst){
					//tao mot thoi khoa bieu
					tbl_ThongTin_TKB = new Tbl_ThongTin_TKB();
					tbl_ThongTin_TKB.setCanBoId(taiKhoan.getCanBoId());
					tbl_ThongTin_TKB.setDotDKId(tbl_DKMH_DotDK);
					tbl_ThongTin_TKB.setNgayCapNhat(new Date());
					
					tbl_ThongTin_TKB.setSiSoMax(tt.getSiso()); //siso
					tbl_ThongTin_TKB.setSiSoMin(0);
					tbl_ThongTin_TKB.setSoHVDangKy(tt.getSoDK()); //sodk
					if(tt.getTuanBD()!=null) tbl_ThongTin_TKB.setTuanBatDau(tt.getTuanBD());//tuanbd
					
					//TODO : cung TKB
					tbl_ThongTin_TKB.setGhiChu(tt.getCungTKB());
					
					tkbid = _tbl_ThongTin_TKBDao.save(tbl_ThongTin_TKB);
					tbl_ThongTin_TKB.setId(tkbid);
					ChumanoLogger.log("---TAO TKB: " + key);
					
					firstTT = tt;
				}
				
				//--------------------------------------
				//--------tao TKB chitiet---------------
				Tbl_ThongTin_TKB_ChiTiet tbl_ThongTin_TKB_ChiTiet = new Tbl_ThongTin_TKB_ChiTiet();
		        //so tiet
				tbl_ThongTin_TKB_ChiTiet.setSoTietHoc(tt.getSoTiet());
		        
				if(tt.getNgayHoc()!=null) tbl_ThongTin_TKB_ChiTiet.setNgayHocId(tt.getNgayHoc());
		        if(tt.getPhongHoc()!=null)tbl_ThongTin_TKB_ChiTiet.setPhongHocId(tt.getPhongHoc());
		        if(tt.getTietBatDau()!=null) tbl_ThongTin_TKB_ChiTiet.setTietBatDauId(tt.getTietBatDau());
		        //thoi khoa bieu
		        tbl_ThongTin_TKB_ChiTiet.setTKBId(tbl_ThongTin_TKB);
				
		        //===them===
		        tbl_ThongTin_TKB_ChiTiets.add(tbl_ThongTin_TKB_ChiTiet);
		        
		        logStr = "TKB_CT: " 
	        		+ "NGAYHOC: "+ ((tt.getNgayHoc()!=null)?tt.getNgayHoc().getMa():"" )  + " - "
	        		+ "PHONGHOC: "+ ((tt.getPhongHoc()!=null)?tt.getPhongHoc().getMa():"" ) + " - "
	        		+ "TIETBD: "+ ((tt.getTietBatDau()!=null)?tt.getTietBatDau().getMa():"" ) + " - "
	        		+ "SOTIET: "+ tt.getSoTiet();
		        ChumanoLogger.log(logStr);
		        //---------------------------------------
				isfirst = false;
			}
			
			//===================================================
			if(tbl_ThongTin_TKB!=null){
				//-----Tao Nhom Lop Mon Hoc------
				Tbl_ThongTin_NhomLop_MonHoc tbl_ThongTin_NhomLop_MonHoc = new Tbl_ThongTin_NhomLop_MonHoc();
				tbl_ThongTin_NhomLop_MonHoc.setMonHocId(firstTT.getMonHoc());
				tbl_ThongTin_NhomLop_MonHoc.setMa(firstTT.getMaNhom());
				tbl_ThongTin_NhomLop_MonHoc.setTen( firstTT.getMonHoc().getMa()+"_"+firstTT.getMaNhom());
				
				tbl_ThongTin_NhomLop_MonHoc.setDotDKId(tbl_DKMH_DotDK);
				tbl_ThongTin_NhomLop_MonHoc.setCanBoId(taiKhoan.getCanBoId());
		        tbl_ThongTin_NhomLop_MonHoc.setNgayCapNhat(new Date());
		        
		        tbl_ThongTin_NhomLop_MonHoc.setTKBId(tbl_ThongTin_TKB);
		       
		        //===them===
		        tbl_ThongTin_NhomLop_MonHocs.add(tbl_ThongTin_NhomLop_MonHoc);
			}
			//--------------------------------
			dics.removeAll(key);
		}
		
		ChumanoLogger.log("===[THEM DU LIEU] FINISHED===");
		ChumanoLogger.log("---LUU TRU DU LIEU---");
		//-------------------------------------------------------------------
		//insert chi tiet TKB
		_tbl_ThongTin_TKB_ChiTietDAO.saveOrUpdateAll(tbl_ThongTin_TKB_ChiTiets);
		//insert nhom lop mon hoc
		_tbl_ThongTin_NhomLop_MonHocDao.saveOrUpdateAll(tbl_ThongTin_NhomLop_MonHocs);
		}
		catch (Exception e) {
			// TODO: handle exception
			   ChumanoLogger.log("--------ERROR: " +e.toString());
			   
			   StringWriter errors = new StringWriter();
			   e.printStackTrace(new PrintWriter(errors));
			   ChumanoLogger.log(errors.toString());
			   ChumanoLogger.log("---------------------------------");
			   
			   //
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
			   
		}
		   
		ChumanoLogger.stop();

	}
	
	private Tbl_CTDT_DM_NgayTrongTuan findNgayHoc(String ma, List<Tbl_CTDT_DM_NgayTrongTuan> list){
		//if(ma==null|| ma.equals("")) return null;
		if(ma==null|| ma.equals("")) ma="0";
		
		for(Tbl_CTDT_DM_NgayTrongTuan obj : list){
			if(obj.getMa().equals(ma)) return obj;
		}
		
		return null;
	}
	
	private Tbl_CTDT_DM_TietHoc findTietHoc(String ma, List<Tbl_CTDT_DM_TietHoc> list){
		//if(ma==null|| ma.equals("")) return null;
		if(ma==null|| ma.equals("")) ma="0";
		
		for(Tbl_CTDT_DM_TietHoc obj : list){
			if(obj.getMa().equals(ma)) return obj;
		}
		
		return null;
	}
	
	private Tbl_CTDT_DM_Phong findPhongHoc(String ma){
		//if(ma==null|| ma.equals("")) return null;
		if(ma==null|| ma.equals("")) ma="UNKN";
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
        	 ConditionReport con = new ConditionReport(); 
             con.setCol("ma");
             con.setValue1(ma);
             con.setOperator("eq");
             cons.add(con);	
        }
        List<String> columns = new ArrayList<String>();
        columns.add("id");
        
        List<Long> phongids = _tbl_CTDT_DM_PhongDao.findColumnByCondition(Tbl_CTDT_DM_Phong.class,cons,columns, 1, 1);
        
        //if(phongids==null||phongids.size()==0) return null;

        Tbl_CTDT_DM_Phong phong;
        if(phongids==null||phongids.size()==0) {
        	Tbl_CTDT_DM_Phong newphong  = new Tbl_CTDT_DM_Phong();
        	newphong.setMa(ma);
        	newphong.setTen(ma);
        	Long id = _tbl_CTDT_DM_PhongDao.save(newphong);
        	newphong.setId(id);
        	
        	phong = newphong;
        }else{
        	phong  = new Tbl_CTDT_DM_Phong();
	        phong.setId(phongids.get(0));
	        phong.setMa(ma);
        }
        return phong;
	}
	
	private Tbl_CTDT_MonHoc findMonHoc(String ma){
		if(ma==null|| ma.equals("")) return null;
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
        	 ConditionReport con = new ConditionReport(); 
             con.setCol("ma");
             con.setValue1(ma);
             con.setOperator("eq");
             cons.add(con);	
        }
        List<String> columns = new ArrayList<String>();
        columns.add("id");
        
        List<Long> ids = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class,cons,columns, 1, 1);
        
        if(ids==null||ids.size()==0) return null;
        //--------------------
        Tbl_CTDT_MonHoc obj  = new Tbl_CTDT_MonHoc();
        obj.setId(ids.get(0));
        obj.setMa(ma);
        
        return obj;
	}
	
	private Tbl_ThongTin_NhomLop_MonHoc findNhomLopMonHoc(Long dotDKid, String maMH, String maNhom){
		if(maMH==null|| maMH.equals("") || maNhom==null|| maNhom.equals("")) return null;
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
        	 ConditionReport con = new ConditionReport(); 
             con.setCol("monHocId.ma");
             con.setValue1(maMH);
             con.setOperator("eq");
             cons.add(con);	
        }
		
		{
       	 ConditionReport con = new ConditionReport(); 
            con.setCol("ma");
            con.setValue1(maNhom);
            con.setOperator("eq");
            cons.add(con);	
        }
		
		{
	       	 ConditionReport con = new ConditionReport(); 
            con.setCol("dotDKId.id");
            con.setValue1(dotDKid);
            con.setOperator("eq");
            cons.add(con);	
	    }

        
        List<Tbl_ThongTin_NhomLop_MonHoc> nlmhs =_tbl_ThongTin_NhomLop_MonHocDao.findByCondition(Tbl_ThongTin_NhomLop_MonHoc.class,cons, 1, 1);
        
        if(nlmhs==null||nlmhs.size()==0) return null;
        //--------------------
        
        return nlmhs.get(0);
	}

	private List<Long> findTKBChiTiet(Long tkbId){
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("TKBId.id");
			con.setValue1(tkbId);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> ids = _tbl_ThongTin_TKB_ChiTietDAO.findColumnByCondition(
				Tbl_ThongTin_TKB_ChiTiet.class, cons, columns, 1, 1);
		return ids;
	}
	//----------------------------------------------
   public final void updatetbl_ThongTin_NhomLop_MonHoc(Tbl_ThongTin_NhomLop_MonHoc obj){
       _tbl_ThongTin_NhomLop_MonHocDao.update(obj);
   }//end update methode

   public final Tbl_ThongTin_NhomLop_MonHoc gettbl_ThongTin_NhomLop_MonHoc (Long id){
       log.info("Try to retrieve the tbl_ThongTin_NhomLop_MonHoc: "+id);
       return get_tbl_ThongTin_NhomLop_MonHocDao().get(id);
   }

   public void deletetbl_ThongTin_NhomLop_MonHoc(Long id){
       log.info("Try to delete the tbl_ThongTin_NhomLop_MonHoc by Id : "+ id);
       if(_tbl_ThongTin_NhomLop_MonHocDao.exists(id)){
	    _tbl_ThongTin_NhomLop_MonHocDao.delete(id);
       } else {
       	throw new IllegalArgumentException("EmployeeId '"+id+"' does not exists");
       }
   }

   public void deleteListtbl_ThongTin_NhomLop_MonHoc(String[] listId){
       for(String id : listId){
   	    deletetbl_ThongTin_NhomLop_MonHoc(Long.parseLong(id));
       }
   }

   public List<Tbl_ThongTin_NhomLop_MonHoc> findtbl_ThongTin_NhomLop_MonHocByCondition(List<ConditionReport> cons,int pageSize,int page){ 
       return _tbl_ThongTin_NhomLop_MonHocDao.findByCondition(Tbl_ThongTin_NhomLop_MonHoc.class, cons, pageSize, page); 
   } 
 
   public int counttbl_ThongTin_NhomLop_MonHocByCondition(List<ConditionReport> cons){ 
       return _tbl_ThongTin_NhomLop_MonHocDao.countByCondition(Tbl_ThongTin_NhomLop_MonHoc.class, cons); 
   } 
 
   public final Tbl_ThongTin_NhomLop_MonHocDAO get_tbl_ThongTin_NhomLop_MonHocDao() {
   	return _tbl_ThongTin_NhomLop_MonHocDao;
   }
   public final void set_tbl_ThongTin_NhomLop_MonHocDao(Tbl_ThongTin_NhomLop_MonHocDAO obj) {
   	this._tbl_ThongTin_NhomLop_MonHocDao = obj;
   }

   public Tbl_DKMH_DotDKDAO get_tbl_DKMH_DotDKDao() {
       return _tbl_DKMH_DotDKDao;
   }
   public void set_tbl_DKMH_DotDKDao(Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao) {
       this._tbl_DKMH_DotDKDao = _tbl_DKMH_DotDKDao;
   }

   public Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
       return _tbl_CTDT_MonHocDao;
   }
   public void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao) {
       this._tbl_CTDT_MonHocDao = _tbl_CTDT_MonHocDao;
   }

   public Tbl_ThongTin_TKBDAO get_tbl_ThongTin_TKBDao() {
       return _tbl_ThongTin_TKBDao;
   }
   public void set_tbl_ThongTin_TKBDao(Tbl_ThongTin_TKBDAO _tbl_ThongTin_TKBDao) {
       this._tbl_ThongTin_TKBDao = _tbl_ThongTin_TKBDao;
   }

   public Tbl_CanBoDAO get_tbl_CanBoDao() {
       return _tbl_CanBoDao;
   }
   public void set_tbl_CanBoDao(Tbl_CanBoDAO _tbl_CanBoDao) {
       this._tbl_CanBoDao = _tbl_CanBoDao;
   }
   
   

}//endclass

class TT_NhomLop_MonHoc{
	private Tbl_CTDT_MonHoc MonHoc;
	private String maNhom;
	
	private Tbl_CTDT_DM_NgayTrongTuan NgayHoc;
	private Tbl_CTDT_DM_Phong PhongHoc;
	private Tbl_CTDT_DM_TietHoc TietBatDau;
	
	private Integer soTiet;
	private Integer siso;
	private Integer soDK;
	private Integer tuanBD;
	
	private String cungTKB;
	
	//------------------------------------
	public Tbl_CTDT_MonHoc getMonHoc() {
		return MonHoc;
	}
	public void setMonHoc(Tbl_CTDT_MonHoc monHoc) {
		this.MonHoc = monHoc;
	}
	public String getMaNhom() {
		return maNhom;
	}
	public void setMaNhom(String maNhom) {
		this.maNhom = maNhom;
	}
	public Tbl_CTDT_DM_NgayTrongTuan getNgayHoc() {
		return NgayHoc;
	}
	public void setNgayHoc(Tbl_CTDT_DM_NgayTrongTuan ngayHoc) {
		NgayHoc = ngayHoc;
	}
	public Tbl_CTDT_DM_Phong getPhongHoc() {
		return PhongHoc;
	}
	public void setPhongHoc(Tbl_CTDT_DM_Phong phongHoc) {
		PhongHoc = phongHoc;
	}
	public Tbl_CTDT_DM_TietHoc getTietBatDau() {
		return TietBatDau;
	}
	public void setTietBatDau(Tbl_CTDT_DM_TietHoc tietBatDau) {
		TietBatDau = tietBatDau;
	}
	public int getSoTiet() {
		return soTiet;
	}
	public void setSoTiet(String soTiet) {
		try{
			this.soTiet = Integer.parseInt(soTiet);
		}catch(Exception e){
			this.soTiet = null;
		}
	}
	public int getSiso() {
		return siso;
	}
	public void setSiso(String siso) {
		try{
			this.siso = Integer.parseInt(siso);
		}catch(Exception e){
			this.siso = null;
		}
	}
	public int getSoDK() {
		return soDK;
	}
	public void setSoDK(String soDK) {
		try{
			this.soDK = Integer.parseInt(soDK);
		}catch(Exception e){
			this.soDK = null;
		}
	}
	public Integer getTuanBD() {
		return tuanBD;
	}
	public void setTuanBD(String tuanBD) {
		try{
			this.tuanBD = Integer.parseInt(tuanBD);
		}catch(Exception e){
			this.tuanBD = null;
		}
	}
	public String getCungTKB() {
		return cungTKB;
	}
	public void setCungTKB(String cungTKB) {
		this.cungTKB = cungTKB;
	}
	

}

